/**
 * 项目名称：EIMClient
 * 类 名 称：NotificationController
 * 类 描 述：(描述信息)
 * 创 建 人：XUYONGJIE
 * 创建时间：2014年12月9日 下午8:52:28
 * 修 改 人：XUYONGJIE
 * 修改时间：2014年12月9日 下午8:52:28
 * 修改备注：
 * @version
 * 
*/
package com.yinhai.util;

import java.util.Date;
import java.util.LinkedHashSet;

import com.cmcc.groupcontacts.MainApp;
import com.yinhai.activity.CallActivity;
import com.yinhai.activity.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

/**
 * @包名：com.littlec.chatdemo.setting
 * @类名：NotificationController
 * @描述：(描述这个类的作用)
 * @作者：XUYONGJIE
 * @时间：2014年12月9日下午8:52:28
 * @版本：1.0.0
 * 
 */
public class NotificationController {
	private static NotificationController controller;
	
	NotificationManager manager;
	
	
	public static final int NEW_MESSAGE_NOTIFICATION=0;
	public static final int NEW_FRIEND_APPLY_NOTIFICATION=1;
	public static final int AGREE_NOTIFICATION=2;
	public static final int REFUSE_NOTIFICATION=3;
	public static final int PHONE_NOTIFICATION=4;
	
	public static final String SINGLE_USER_OR_GROUP_OR_SYSTEM_NOTIFICATION="single_user_or_group_or_system_notification";
	public static final int SINGLE_USER_NOTIFICATION=0;
	public static final int GROUP_NOTIFICATION=1;
	public static final int SYSTEM_NOTIFICATION=2;
	
	public static final String NOTIFICATION_ACTION="notification_action_xuyongjie";
	public static final String NOTIFICATION_TYPE="notification_type";
	public static final String SENDER_COUNT="sender_count";
	public static final String SINGLE_SENDER_USERNAME="single_sender_username";
	public static final String PHONE_NOTIFICATION_ACTION="notification_action_phone";
	
	public static final String NAVIAGTE_TO_MAINTABACTIVITY_SELECTED_TABINDEX="navigate_to_maintabactivity_seleted_tabindex";
	
	
	
	public static final long TIME_SPAN=2000;
	
	private NotificationController(){
		manager=(NotificationManager)MainApp.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
		init();
	};
	
	public static NotificationController getInstance()
	{
		if(controller==null)
		{
			controller=new NotificationController();
		}
		return controller;
	}
	
	public static LinkedHashSet<String> userNameHashSet;
	public static int msgCount=0;
	
	private long firstTime=0;
	private long secondTime=0;

	private LinkedHashSet<String> ackuserNameHashSet;
	
	
	public void showNotification(int notificationType,int singleUserOrGroupOrSystemNotification,String fromUserName,String fromName)
	{
        Context context = MainApp.getContext();
//		if(!SettingEntity.getInstance().getWeatherAcceptMessageNotification())//不接收消息通知
//		{
//			return;
//		}
		if(notificationType==NEW_MESSAGE_NOTIFICATION)
		{
			msgCount++;
		}
		if (notificationType != NEW_MESSAGE_NOTIFICATION ) {
			ackuserNameHashSet.add(fromUserName);
		}
		userNameHashSet.add(fromName);
		String title=getTitle(notificationType);
		String subTitle=getSubtitle(notificationType);
		Notification notification=new Notification();
		secondTime=new Date().getTime();
		if(secondTime-firstTime<TIME_SPAN)
		{
			//时间间隔内多条消息，不震动，不响铃
		}else {
	        if(SettingEntity.getInstance().getIsVibrateNotificationOn())
	        {
	        	notification.vibrate=new long[]{50,100,50,100};
	        }
	        if(SettingEntity.getInstance().getIsVoiceNotificationOn())
	        {
	        	notification.defaults=Notification.DEFAULT_SOUND;
	        }
		}
		firstTime=secondTime;
		notification.icon=R.drawable.icon;
		notification.tickerText=title;
		notification.when=System.currentTimeMillis();
		notification.flags=Notification.FLAG_AUTO_CANCEL;
		Intent broadcastIntent=new Intent(NOTIFICATION_ACTION);
		Bundle bundle=new Bundle();
		bundle.putInt(NOTIFICATION_TYPE, notificationType);
		bundle.putInt(SENDER_COUNT,userNameHashSet.size());
		bundle.putString(SINGLE_SENDER_USERNAME, fromUserName);
		bundle.putInt(SINGLE_USER_OR_GROUP_OR_SYSTEM_NOTIFICATION, singleUserOrGroupOrSystemNotification);
		broadcastIntent.putExtras(bundle);
		PendingIntent pendingIntent=PendingIntent.getBroadcast(context,notificationType, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		notification.setLatestEventInfo(context, title, subTitle, pendingIntent);
		manager.notify((notificationType==NEW_MESSAGE_NOTIFICATION)?notificationType:NEW_FRIEND_APPLY_NOTIFICATION, notification);
	}
	
	public void init()
	{
		firstTime=0;
		secondTime=0;
		userNameHashSet=new LinkedHashSet<String>();
		ackuserNameHashSet = new LinkedHashSet<String>();
		msgCount=0;
	}
	
	private String getTitle(int notificationType)
	{
		String result="";
		switch (notificationType) {
		case NEW_MESSAGE_NOTIFICATION:
			result="收到新消息（共"+msgCount+"条）";
			break;
		case NEW_FRIEND_APPLY_NOTIFICATION:
			result="收到好友请求";
			break;
		case AGREE_NOTIFICATION:
			result="同意好友请求";
			break;
		case REFUSE_NOTIFICATION:
			result="拒绝好友请求";
			break;
		default:
			break;
		}
		return result;
	}
	private String getSubtitle(int notificationType)
	{
		String result="";
		switch (notificationType) {
		case NEW_MESSAGE_NOTIFICATION:
			result=getMultiSenderSubtitle();
			break;
		case NEW_FRIEND_APPLY_NOTIFICATION:
			int count=1;
			if(count<=1)
			    result=userNameHashSet.toArray()[userNameHashSet.size()-1]+"请求加你为好友";
			else 
				result="已有"+/*count+*/"多个联系人申请添加你为好友"; //本地可能ABC申请，AD再申请，是2或4个人
			break;
		case AGREE_NOTIFICATION:
			result=userNameHashSet.toArray()[userNameHashSet.size()-1]+"同意你的好友请求";
			break;
		case REFUSE_NOTIFICATION:
			result=userNameHashSet.toArray()[userNameHashSet.size()-1]+"拒绝你的好友请求";
			break;
		default:
			break;
		}
		return result;
	}
	
	private String getMultiSenderSubtitle()
	{
		String result="来自";
		int index=0;
		for (String name : userNameHashSet) {
			if (ackuserNameHashSet.contains(name)) {
				continue;
			}
			if(index>=2)
			{
				break;
			}
			if(index==0)
			{
				result+=name;
			}else {
				result+="、"+name;
			}
			index++;
		}
		return index>1?result+"等":result;
	}
	
	public void cancelAllNotification()
	{
		init();
		manager.cancelAll();
	}
	
	public void cancelNotification(int id)
	{
		init();
		manager.cancel(id);
	}
	
	public void showPhoneNotification(int type){//type 1:single   2:conference
	    Context context =MainApp.getContext();
		Notification notification = new Notification();
		notification.icon=R.drawable.icon;
		notification.tickerText=context.getString(R.string.app_name);
        notification.when=System.currentTimeMillis();
		notification.flags |= Notification.FLAG_ONGOING_EVENT;
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.defaults=Notification.DEFAULT_SOUND;
		notification.tickerText="正在通话中";
		Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
	    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
	    if(type==1)
	       notificationIntent.setClass(context,CallActivity.class); 
	    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
//		Intent notificationIntent = new Intent();
//		notificationIntent.setAction(PHONE_NOTIFICATION_ACTION);
		PendingIntent pendingIntent=PendingIntent.getActivity(context,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
		notification.setLatestEventInfo(context,"正在通话中","正在通话中",pendingIntent);
		manager.notify(PHONE_NOTIFICATION, notification);
	}
	
	public void cancelPhoneNotification(){
		   cancelNotification(PHONE_NOTIFICATION);
	}
}
